﻿#include "stopstate.h"

#include "common_log.h"
#include "component_data.h"

StopState::StopState(QObject *parent)
	: SystemStateBase(parent)
{
}

StopState::~StopState()
{
}

bool StopState::InitState()
{
	/*dialog = SystemDialogManger::GetSystemExceptionDialog();
	if (!dialog->InitDialog()) {
		log_error(" system exception dialog init  failed");
		return false;
	}*/
	return true;
}


bool StopState::Running()
{
	stopFlag = false;
	log_trace("***************SYSTEM STATE STOP***************");
	m_writer->WriteTraceJournal(u8"状态异常，系统停止");
	//dialog->ShowDialog();

	//m_handler->ShowErrorWid(errmsg, errcode);
	return true;
}

void StopState::ComponentEvnetHandle(int event)
{
	if (stopFlag) {
		return;
	}

	log_trace("event = {}", event);

	// 1. 响应复位按钮事件
	if (event == (int)ComponentEvent::system_event_reset) {
		if (!m_com->ResetComponent()) {

			QString str = u8"组件复位失败：";
			m_writer->WriteWarnJournal(str);
			str += m_com->GetErrorMsg();
			log_error("component reset failed, {}", str);
			//dialog->StopDialog();
			//dialog->ShowDialog();
		} else {
			m_writer->WriteTraceJournal(u8"组件复位成功");
			log_trace("component reset success");
			//dialog->StopDialog();
			emit ChangeSystemState(1); // 切换到初始状态
		}
		return;
	} 

	// 启动按钮 切换到工作模式
	if ((int)ComponentEvent::system_event_start == event) {
		m_writer->WriteTraceJournal("停止模式，无法执行启动操作，请先复位");
		return;
	}
	if ((int)ComponentEvent::system_event_stop == event) {
		QString str = m_com->GetErrorMsg();
		m_writer->WriteErrorJournal(QString(u8"异常：%1").arg(str));
	}
}
